// miniprogram/pages/adminclass/adminclass.js
import Dialog from '/@vant/weapp/dialog/dialog';
const app = getApp()
const db = wx.cloud.database()
const uopenid = app.globalData.openid

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  creat() {
    wx.redirectTo({
      url: './creatclass',
    })
  },
  onLoad(options) {
    var that = this
    if (app.globalData.openid) {
      console.log(app.globalData.openid)
      db.collection('adminclass').where({
        _openid: app.globalData.openid
      }).orderBy('time', 'desc').get({
        success: res => {
          that.setData({
            adminclass: res.data
          })
          console.log(that.data.adminclass)
        }
      })
    }
    db.collection('adminclass').where({
      _openid: app.globalData.openid
    }).count({
      success: res => {
        this.setData({
          all: res.total
        })
      }
    })
  },
  load: function() { // 分页加载
    console.log('jiazai2222')
    this.setData({
      load: true
    })
    let that = this
    let shu = 20
    let dir = that.data.adminclass
    that.data.page = that.data.page + 20
    const db = wx.cloud.database()
    db.collection('adminclass').skip(this.data.page).limit(shu).where({
      number: this.data.number
    }).orderBy('time', 'desc').get({
      success: res => {
        dir = dir.concat(res.data)
        this.data.adminclass = dir
        this.setData({
          adminclass: res.data,
          load: false
        })
      },
      fail: err => {
        wx.showToast({
          icon: "none",
          title: '人家也是有底线的哦！',
        })
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading()
    this.onLoad()
    wx.hideNavigationBarLoading();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.page2 <= this.data.all) {
      wx.hideNavigationBarLoading(); //隐藏加载
      wx.stopPullDownRefresh();
      this.load()
    }
  },
  onClose(event) {
    const {
      position,
      instance
    } = event.detail;
    switch (position) {
      case 'left':
      case 'cell':
        instance.close();
        break;
      case 'right':
        Dialog.confirm({
          message: '确定删除吗？'
        }).then(() => {
          console.log('点击了确定')
          instance.close();
        }).catch(() => {
          // on cancel
          console.log('取消了')
        });;
        break;
    }
  },
  tap: function(e) {
    wx.navigateTo({
      url: './classdetail?_id=' + e.target.id + "&number=" + e.target.dataset.number,
    })
  }
})